package com.example.qsindusty.jmm_spline.OrdeFragment;


import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.yixin.ystartlibrary.base.BasePresenter;
import com.yixin.ystartlibrary.base.IBaseView;
import com.yixin.ystartlibrary.utils.LogUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administration on 2018/3/6.
 */

public class OrderPresenter extends BasePresenter implements  OrderCotract.Persenter {
    OrderCotract.View mView ;
    @Override
    public void bindView(IBaseView view) {
        mView = (OrderCotract.View) view;
    }


    @Override
    public void unbindView() {
        dispose();
    }

    @Override
    public void getOrder(String token,String status ,String rows,String page) {
        LogUtils.e("token:"+token+"status:"+status+"rows=="+rows+"page"+page);

        ApiManager.getInstence().getJmmApi().getOrder(token,status,rows,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(OrderData jsonElement) {

                    if (jsonElement.getStatus().equals("0")){
                        if (jsonElement.getCount()!=0)
                        mView.refresshDataSuccessful(jsonElement);
                        else mView.refreshNoMore(jsonElement.getMessage());
                    }else if (jsonElement.getStatus().equals("1")){
                        mView.nologin();
                    }else if (jsonElement.getStatus().equals("2")){
                        mView.refreshNoMore(jsonElement.getMessage());
                    }
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.e("json=="+e.getMessage()+e.getLocalizedMessage());
                mView.refreshDataFraier(e.getMessage().toString());

            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public void markOrder(String token, String ordernum, final String status,String msg, final int position) {
        LogUtils.i("token:"+token+"num:"+ordernum+"status:"+status);
/*
        ApiManager.getInstence().getJmmApi().markOrder(token,ordernum,status,msg)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(BaseData jsonElement) {
                        LogUtils.i(jsonElement.toString());
                        if (TextUtils.equals(jsonElement.getStatus(),"0"))
                            mView.markSuccessful(jsonElement.getMessage(),status,position);
                        else
                            mView.markFairer(jsonElement.getMessage());
                        mView.closeLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.markFairer(e.getMessage());
                       mView.closeLoading();
                    }

                    @Override
                    public void onComplete() {
            mView.closeLoading();
                    }
                });
*/
    }


}
